<?php include _include(ADMIN_PATH.'view/htm/header.inc.htm');?>

<div class="row">
	<div class="col-xs-12">
		<div class="btn-group m-b-1" role="group">
			<?php echo admin_tab_active($menu['pay']['tab'], 'shops');?>
		</div>
	</div>
</div>

<div class="row">
	<div class="col-lg-12">
		<div class="card">
			<div class="table-responsive">
				<table id="shops" lay-filter="shops_filter"></table>
				<!--<button class="layui-btn layui-btn-fluid" id="cateAdd">添加店铺</button>-->
				<!--<table id="cate" lay-filter="cate_filter"></table>-->
			</div>
			<hr class="m-t-0" />
		</div>
	</div>
</div>

<?php include _include(ADMIN_PATH.'view/htm/footer.inc.htm');?>
<script type="text/html" id="shopsDemo">
	<a class="layui-btn layui-btn-xs" lay-event="detail">管理店铺</a>
</script>
<script type="text/html" id="cateDemo">
	<a class="layui-btn layui-btn-xs" lay-event="detail">管理价格段</a>
	<!--<a class="layui-btn layui-btn-xs" lay-event="del">删除店铺</a>-->
</script>
<script type="text/html" id="goodsDemo">
	<a class="layui-btn layui-btn-xs" lay-event="detail">管理商品</a>
	<!--<a class="layui-btn layui-btn-xs" lay-event="del">删除价格区间</a>-->
</script>
<script type="text/html" id="goodsPDemo">
	<!--<a class="layui-btn layui-btn-xs" lay-event="del">删除商品</a>-->
</script>
<script>
	var shopsTable;
    var cateTable;
    var goodsTable;
    var goodsPTable;
    var form;
    var indexPrice;
    var cateAdd;
    layui.use('form', function() {
        form = layui.form;
        form.render();
        form.on('submit(addCateFilter)', function(data){
            $.ajax({
                type: "post",
                data:data.field,
                async: false,
                url: "<?php echo url('api-cateAdd'); ?>",
                success: function (data) {
                    if (data.status){
                        layer.msg(data.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                        cateTable.reload();
                        layer.close(cateAdd);
                    }else {
                        layer.msg(data.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                    }
                }
            });
            return false;
        });
        form.on('submit(addPriceFilter)', function(data){
            $.ajax({
                type: "post",
                data:data.field,
                async: false,
                url: "<?php echo url('api-priceAdd'); ?>",
                success: function (data) {
                    if (data.status){
                        layer.msg(data.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                        goodsTable.reload();
                        layer.close(indexPrice);
                    }else {
                        layer.msg(data.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                    }
                }
            });
            return false;
        });
        form.on('submit(addGoodsFilter)', function(data){
            $.ajax({
                type: "post",
                data:data.field,
                async: false,
                url: "<?php echo url('api-goodsAdd'); ?>",
                success: function (data) {
                    if (data.status){
                        layer.msg(data.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                        goodsPTable.reload();
                        layer.close(indexPrice);
                    }else {
                        layer.msg(data.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                    }
                }
            });
            return false;
        });
    });





    layui.use('table', function(){
        var table = layui.table;
        shopsTable = table.render({
            elem: '#shops'
            ,url: '/adminqbvodnn/api-shops.htm' //数据接口
            ,cols: [[ //表头
                {type:'numbers',title: '序列号', width:60}
                ,{field: 'uid', title: '用户id', width:200}
                ,{field: 'username', title: '用户名', width:200}
                ,{field: 'email', title: '用户邮箱', width:200}
                ,{fixed: 'right',width:120, align:'left', title: '操作', toolbar: '#shopsDemo'}
            ]]
        });

        table.on('tool(shops_filter)', function(obj){
            var data = obj.data;
            var layEvent = obj.event;
            if(layEvent === 'detail'){
                layer.open({
                    id:'shopsDiv',
                    type: 1,
                    title: '店铺',
                    area: ['600px', '300px'],
                    anim: 1
                    ,content:'<button class="layui-btn layui-btn-fluid" id="cateAdd" data-id="'+obj.data.uid+'">添加店铺</button><div id="cate" lay-filter="cate_filter"></div>'
                });
                cateTable = table.render({
                    elem: '#cate'
                    ,url: "<?php echo url('api-cate'); ?>"
                    ,where: data
                    ,cols: [[ //表头
                        {type:'numbers',title: '序列号', width:60}
                        ,{field: 'name', title: '商铺名', width:200,edit:'text'}
                        ,{fixed: 'right',width:200, align:'left', title: '操作', toolbar: '#cateDemo'}
                    ]]
                });
                $("#cateAdd").on('click',function () {
                    var uid = $(this).attr('data-id');
                    cateAdd = layer.open({
                        type: 1,
                        title: '添加店铺',
                        id: 'addCate',
                        area: ['360px'],
                        anim: 1
                        ,content: '<form class="layui-form layui-form-pane">\n' +
                            '    <div class="layui-form-item">\n' +
                            '        <label class="layui-form-label">店铺名字</label>\n' +
                            '        <div class="layui-input-block">\n' +
                            '            <input type="text" name="name" lay-verify="required" placeholder="只能为字母和数字，下划线_及破折号-" autocomplete="off" class="layui-input">\n' +
                            '            <input type="hidden" name="uid"value="'+uid+'" class="layui-input">\n' +
                            '        </div>\n' +
                            '    </div>\n' +
                            '    <div class="layui-form-item">\n' +
                            '        <div class="layui-input-block">\n' +
                            '            <button class="layui-btn" lay-submit lay-filter="addCateFilter">立即提交</button>\n' +
                            '        </div>\n' +
                            '    </div>\n' +
                            '    <!-- 更多表单结构排版请移步文档左侧【页面元素-表单】一项阅览 -->\n' +
                            '</form>\n' +
                            '\n' +
                            '\n'
                    });

                    form.render();
                });
            }
        });

        table.on('tool(cate_filter)', function(obj){
            var data = obj.data;
            var layEvent = obj.event;
            if(layEvent === 'detail'){
                layer.open({
                    id:'goodsPDiv',
                    type: 1,
                    title: '价格区间',
                    area: ['600px', '300px'],
                    anim: 1
                    ,content:'<button class="layui-btn layui-btn-fluid" id="priceAdd" data-id="'+obj.data.id+'" data-uid="'+obj.data.uid+'">添加价格段</button><div id="goods" lay-filter="goods_filter"></div>'
                });
                goodsTable = table.render({
                    elem: '#goods'
                    ,url: "<?php echo url('api-goods'); ?>"
                    ,where: data
                    ,cols: [[
                        {type:'numbers',title: '序列号', width:60}
                        ,{field: 'price_start', title: '起始价格', width: 200,edit:'text'}
                        ,{field: 'price_end', title: '结束价格', width: 200,edit:'text'}
                        ,{fixed: 'right', align:'left',width: 200, toolbar: '#goodsDemo'}
                    ]]
                });
                $("#priceAdd").on('click',function () {
                    var cid = $(this).attr('data-id');
                    var uid = $(this).attr('data-uid');
                    indexPrice = layer.open({
                        type: 1,
                        title: '添加价格区间',
                        id: 'addPrice',
                        area: ['360px'],
                        anim: 1
                        ,content: '<form class="layui-form layui-form-pane">\n' +
                            '    <div class="layui-inline">\n' +
                            '    <label class="layui-form-label">价格范围</label>\n' +
                            '    <div class="layui-input-inline" style="width: 100px;">\n' +
                            '      <input type="text" name="price_start" placeholder="￥最初价格" autocomplete="off" class="layui-input">' +
                            '<input type="hidden" name="cid" value="'+cid+'" class="layui-input">\n' +
                            '<input type="hidden" name="uid" value="'+uid+'" class="layui-input">\n' +
                            '    </div>\n' +
                            '    <div class="layui-form-mid">-</div>\n' +
                            '    <div class="layui-input-inline" style="width: 100px;">\n' +
                            '      <input type="text" name="price_end" placeholder="￥结束价格" autocomplete="off" class="layui-input">\n' +
                            '    </div>\n' +
                            '  </div>\n' +
                            '    <div class="layui-form-item">\n' +
                            '        <div class="layui-input-block">\n' +
                            '            <button class="layui-btn" lay-submit lay-filter="addPriceFilter">立即提交</button>\n' +
                            '        </div>\n' +
                            '    </div>\n' +
                            '    <!-- 更多表单结构排版请移步文档左侧【页面元素-表单】一项阅览 -->\n' +
                            '</form>\n' +
                            '\n' +
                            '\n'
                    });
                    form.render();
                });
            } else if(layEvent === 'del'){
                layer.confirm('真的删除店铺吗？', function(index){
                    layer.close(index);
                    $.ajax({
                        type: "post",
                        url: "<?php echo url('api-storeDel'); ?>",
                        data: data,
                        async: false,
                        success: function(result) {
                            if (result.status === true) {
                                obj.del();
                                layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                            } else {
                                layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i>');
                            }
                        }
                    });

                });
            }
        });

        table.on('edit(cate_filter)', function(obj){
            var data ={};
            data.id = obj.data.id;
            data.uid = obj.data.uid;
            data.name = obj.field;
            data.value = obj.value;
            $.ajax({
                type: "post",
                url: "<?php echo url('api-storeUpdate'); ?>",
                data: data,
                async: false,
                success: function(result) {
                    if (result.status === true) {
                        layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                    } else {
                        cateTable.reload();
                        layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i>');
                    }
                }
            });
        });
        table.on('tool(goods_filter)', function(obj){
            var data = obj.data;
            var layEvent = obj.event;
            if(layEvent === 'detail'){
                layer.open({
                    id:'goodsDiv',
                    type: 1,
                    title: '商品管理',
                    area: ['600px', '300px'],
                    anim: 1
                    ,content:'<button class="layui-btn layui-btn-fluid" id="goodsAdd" data-id="'+obj.data.id+'" data-idx="'+obj.data.cid+'">添加商品</button><div id="goodsP" lay-filter="goodsP_filter"></div>'
                });
                goodsPTable = table.render({
                    elem: '#goodsP'
                    ,url: "<?php echo url('api-goodsP'); ?>"
                    ,where: data
                    ,cols: [[
                        {type:'numbers',title: '序列号', width:60}
                        ,{field: 'name', title: '商品名', width: 200,edit:'text'}
                        ,{fixed: 'right', align:'left',width: 100, toolbar: '#goodsPDemo'}
                    ]]
                });
                $("#goodsAdd").on('click',function () {
                    var pid = $(this).attr('data-id');
                    var cid = $(this).attr('data-idx');
                    indexPrice = layer.open({
                        type: 1,
                        title: '添加商品',
                        id: 'addGoods',
                        area: ['360px'],
                        anim: 1
                        ,content: '<form class="layui-form layui-form-pane">\n' +
                            '    <div class="layui-form-item">\n' +
                            '        <label class="layui-form-label">商品名</label>\n' +
                            '        <div class="layui-input-block">\n' +
                            '            <input type="text" name="name" lay-verify="required" placeholder="只能为字母和数字，下划线_及破折号-" autocomplete="off" class="layui-input">\n' +
                            '            <input type="hidden" name="pid" value="'+pid+'" lay-verify="required" class="layui-input">\n' +
                            '            <input type="hidden" name="cid" value="'+cid+'" lay-verify="required"  class="layui-input">\n' +
                            '        </div>\n' +
                            '    </div>\n' +
                            '    <div class="layui-form-item">\n' +
                            '        <div class="layui-input-block">\n' +
                            '            <button class="layui-btn" lay-submit lay-filter="addGoodsFilter">立即提交</button>\n' +
                            '        </div>\n' +
                            '    </div>\n' +
                            '    <!-- 更多表单结构排版请移步文档左侧【页面元素-表单】一项阅览 -->\n' +
                            '</form>\n' +
                            '\n' +
                            '\n'
                    });
                    form.render();
                })
            } else if(layEvent === 'del'){
                layer.confirm('真的删除价格区间吗？', function(index){
                    layer.close(index);
                    $.ajax({
                        type: "post",
                        url: "<?php echo url('api-goodsDel'); ?>",
                        data: data,
                        async: false,
                        success: function(result) {
                            if (result.status === true) {
                                obj.del();
                                layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                            } else {
                                layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i>');
                            }
                        }
                    });

                });
            }
        });
        table.on('edit(goods_filter)', function(obj){
            console.log(obj);
            var data ={};
            data.id = obj.data.id;
            data.cid = obj.data.cid;
            data.name = obj.field;
            data.value = obj.value;
            $.ajax({
                type: "post",
                url: "<?php echo url('api-goodsUpdate'); ?>",
                data: data,
                async: false,
                success: function(result) {
                    if (result.status === true) {
                        layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                    } else {
                        cateTable.reload();
                        layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i>');
                    }
                }
            });
        });

        table.on('tool(goodsP_filter)', function(obj){
            var data = obj.data;
            var layEvent = obj.event;
            if(layEvent === 'del'){
                layer.confirm('真的删除吗？', function(index){
                    layer.close(index);
                    $.ajax({
                        type: "post",
                        url: "<?php echo url('api-goodsPDel'); ?>",
                        data: data,
                        async: false,
                        success: function(result) {
                            if (result.status === true) {
                                obj.del();
                                layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                            } else {
                                layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i>');
                            }
                        }
                    });
                });
            }
        });

        table.on('edit(goodsP_filter)', function(obj){
            var data ={};
            data.id = obj.data.id;
            data.cid = obj.data.cid;
            data.pid = obj.data.pid;
            data.name = obj.field;
            data.value = obj.value;
            $.ajax({
                type: "post",
                url: "<?php echo url('api-goodsPUpdate'); ?>",
                data: data,
                async: false,
                success: function(result) {
                    if (result.status === true) {
                        layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                    } else {
                        cateTable.reload();
                        layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i>');
                    }
                }
            });
        });
    });


</script>
<script>

$('.submit').click(function(){
	var jform = $(this).closest('form');
	var postdata = jform.serialize();
	$.xpost(jform.attr('action'), postdata, function(code, message){
		$.alert(message);
		if(code==1) setTimeout(function(){window.location.reload()}, 1500);
	});
	return false;
});

var jconfirm = $("#confirm");
var juserlist = $('#userlist');
// 删除选中的记录
jconfirm.on('click', function(){
	var jchecked = juserlist.find('input[name="delete"]').filter(function(){ return this.checked; });
	if(jchecked.length < 1) return alert("请选择要删除的分类");
	if(!window.confirm("确认删除？")) return false;
	jchecked.each(function() {
		jtr = $(this).parents('tr');
		var uid = jtr.attr('uid');
		$.xpost(xn.url('pay-catedel'), {id:uid}, function(code, message) {
			if(code != 0) $.alert(message);
			jtr.remove();
		});
	});
});

$('#nav_pc li.pay, #nav_mobile li.pay').tab('show');
</script>